package cn.itcast.mq.email.listener;

import cn.itcast.util.MailUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;

import java.io.IOException;
import java.util.Map;

public class BirthdayEmailListener implements MessageListener {

    //当读取到队列有值时触发，发送邮件
    @Override
    public void onMessage(Message message) {
        byte[] body = message.getBody();
        ObjectMapper objectMapper = new ObjectMapper();

        try {
            Map<String, String> map = objectMapper.readValue(body,Map.class);
            String userName = map.get("userName");
            String email = map.get("email");
            String html = map.get("html");

            //发送邮件
            System.out.println("BirthdayEmailListener发送邮件。。。。");
            MailUtil.sendBirthdayWishes(userName,email,html);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
